using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.Services.TfsAlert.Core
{
    [TestClass]
	public class TfsServerInfoHelper_UnitTest
    {
        [TestMethod]
        public void AssertServerInfoConstructorExceptionsThrownProperly()
        {
            TfsServerInfoHelper helper;

            try
            {
                helper = new TfsServerInfoHelper(null);
                Assert.Fail("TfsServerInfoHelper constructor should throw an ArgumentException if empty or null");
            }
            catch (ArgumentException) { } // Should end up here
            catch (Exception)
            {
                Assert.Fail("TfsServerInfoHelper constructor should throw an ArgumentException (specifically) if empty or null");
            }

            try
            {
                helper = new TfsServerInfoHelper("");
                Assert.Fail("TfsServerInfoHelper constructor should throw an ArgumentException if empty or null");
            }
            catch (ArgumentException) { } // Should end up here
            catch (Exception)
            {
                Assert.Fail("TfsServerInfoHelper constructor should throw an ArgumentException (specifically) if empty or null");
            }

            try
            {
                helper = new TfsServerInfoHelper("   ");
                Assert.Fail("TfsServerInfoHelper constructor should throw an ArgumentException if empty or null");
            }
            catch (ArgumentException) { } // Should end up here
            catch (Exception)
            {
                Assert.Fail("TfsServerInfoHelper constructor should throw an ArgumentException (specifically) if empty or null");
            }

            try
            {
                helper = new TfsServerInfoHelper("http");
                Assert.Fail("TfsServerInfoHelper constructor should throw an ArgumentException if the URI string is invalid");
            }
            catch (ArgumentException) { } // Should end up here
            catch (Exception)
            {
                Assert.Fail("TfsServerInfoHelper constructor should throw an ArgumentException (specifically) if empty or null");
            }

            try
            {
                helper = new TfsServerInfoHelper("http:");
                Assert.Fail("TfsServerInfoHelper constructor should throw an ArgumentException if the URI string is invalid");
            }
            catch (ArgumentException) { } // Should end up here
            catch (Exception)
            {
                Assert.Fail("TfsServerInfoHelper constructor should throw an ArgumentException (specifically) if empty or null");
            }

            try
            {
                helper = new TfsServerInfoHelper("http:///servername");
                Assert.Fail("TfsServerInfoHelper constructor should throw an ArgumentException if the URI string is invalid");
            }
            catch (ArgumentException) { } // Should end up here
            catch (Exception)
            {
                Assert.Fail("TfsServerInfoHelper constructor should throw an ArgumentException (specifically) if empty or null");
            }
        }


        [TestMethod]
        public void AssertServerInfoExtractedProperly()
        {
            TfsServerInfoHelper helper;

            helper = new TfsServerInfoHelper("http://servername");

            Assert.AreEqual<int>(helper.Port, 80);
            Assert.AreEqual<string>(helper.Protocol, "http");
            Assert.AreEqual<string>(helper.Server, "servername");

            helper = new TfsServerInfoHelper("http://servername:");

            Assert.AreEqual<int>(helper.Port, 80);
            Assert.AreEqual<string>(helper.Protocol, "http");
            Assert.AreEqual<string>(helper.Server, "servername");

            helper = new TfsServerInfoHelper("http://servername1:8080");

            Assert.AreEqual<int>(helper.Port, 8080);
            Assert.AreEqual<string>(helper.Protocol, "http");
            Assert.AreEqual<string>(helper.Server, "servername1");

            helper = new TfsServerInfoHelper("http://servername2:80/");

            Assert.AreEqual<int>(helper.Port, 80);
            Assert.AreEqual<string>(helper.Protocol, "http");
            Assert.AreEqual<string>(helper.Server, "servername2");

            try
            {
                helper = new TfsServerInfoHelper("http://servername3:80800");
                Assert.Fail("TfsServerInfoHelper constructor should throw an ArgumentException if the port number is out of range");
            }
            catch (ArgumentException) { }

            helper = new TfsServerInfoHelper("http://servername4:8080/");

            Assert.AreEqual<int>(helper.Port, 8080);
            Assert.AreEqual<string>(helper.Protocol, "http");
            Assert.AreEqual<string>(helper.Server, "servername4");

            helper = new TfsServerInfoHelper("https://servername5/");

            Assert.AreEqual<int>(helper.Port, 80);
            Assert.AreEqual<string>(helper.Protocol, "https");
            Assert.AreEqual<string>(helper.Server, "servername5");

            helper = new TfsServerInfoHelper("https://servername6:443/");

            Assert.AreEqual<int>(helper.Port, 443);
            Assert.AreEqual<string>(helper.Protocol, "https");
            Assert.AreEqual<string>(helper.Server, "servername6");

            helper = new TfsServerInfoHelper("https://servername7:");

            Assert.AreEqual<int>(helper.Port, 80);
            Assert.AreEqual<string>(helper.Protocol, "https");
            Assert.AreEqual<string>(helper.Server, "servername7");

        }
    }
}
